/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.PluginReview;
import me.zhengjie.modules.system.domain.vo.PluginReviewCreateVo;
import me.zhengjie.modules.system.domain.vo.PluginReviewQueryCriteria;
import me.zhengjie.modules.system.service.PluginReviewService;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author lxp
* @date 2024-11-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "PluginReviewController管理")
@RequestMapping("/api/pluginReview")
public class PluginReviewController {

    private final PluginReviewService pluginReviewService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('pluginReview:list')")
    public void exportPluginReview(HttpServletResponse response, PluginReviewQueryCriteria criteria) throws IOException {
        pluginReviewService.download(pluginReviewService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询PluginReviewController")
    @ApiOperation("查询PluginReviewController")
    //@PreAuthorize("@el.check('pluginReview:list')")
    public ResponseEntity<PageResult<PluginReview>> queryPluginReview(PluginReviewQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(pluginReviewService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping("/{id}")
    @Log("插件评论详情")
    @ApiOperation("插件评论详情")
    //@PreAuthorize("@el.check('pluginReview:list')")
    public ResponseEntity<PluginReview> detail(@PathVariable Long id){
        return new ResponseEntity<>(pluginReviewService.queryPluginDetailById(id),HttpStatus.OK);
    }

    @GetMapping("/listByParent/{parentId}")
    @Log("根据插件id查询评论列表")
    @ApiOperation("根据插件id查询评论列表")
    //@PreAuthorize("@el.check('pluginReview:list')")
    public ResponseEntity<List<PluginReview>> queryPluginReview(@PathVariable Long parentId,@RequestParam(required = false) String status){
        return new ResponseEntity<>(pluginReviewService.queryByParentId(parentId, status),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增评论")
    @ApiOperation("新增评论")
    //@PreAuthorize("@el.check('pluginReview:add')")
    public ResponseEntity<Object> createPluginReview(@Validated @RequestBody PluginReviewCreateVo resources){
        pluginReviewService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改PluginReviewController")
    @ApiOperation("修改PluginReviewController")
    //@PreAuthorize("@el.check('pluginReview:edit')")
    public ResponseEntity<Object> updatePluginReview(@Validated @RequestBody PluginReview resources){
        pluginReviewService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除PluginReviewController")
    @ApiOperation("删除PluginReviewController")
    //@PreAuthorize("@el.check('pluginReview:del')")
    public ResponseEntity<Object> deletePluginReview(@RequestBody List<Long> ids) {
        pluginReviewService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}